package cn.com.dhc.danlu.activity.home.wrap;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yiji.www.frameworks.utils.DensityUtils;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.TipCountUtil;
import cn.com.dhc.danlu.widget.BadgeView;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-08-04
 * Time: 09:10
 * Description: 首页顶部视图 封装
 */
public class HomeTopHolder implements View.OnClickListener {
    public static final int CLASSIFY = R.id.tv_classify;
    public static final int MSG = R.id.ll_msg;
    public static final int SEARCH = R.id.rl_search;
    private PopManager mPopManager;
    private BadgeView bv_msg_count;
    private View mHomeTopView;
    private TextView tv_classify;
    private LinearLayout ll_msg;
    private ImageView iv_msg_flag;
    private RelativeLayout rl_search;
    private OnOptionClickListener onOptionClickListener;

    public HomeTopHolder(@NonNull View parentView) {
        this.mHomeTopView = parentView;
        tv_classify = (TextView) parentView.findViewById(R.id.tv_classify);
        ll_msg = (LinearLayout) parentView.findViewById(R.id.ll_msg);
        iv_msg_flag = (ImageView) parentView.findViewById(R.id.iv_msg_flag);
        rl_search = (RelativeLayout) parentView.findViewById(R.id.rl_search);
        bindAction();
    }

    public void setOnOptionClickListener(OnOptionClickListener onOptionClickListener) {
        this.onOptionClickListener = onOptionClickListener;
    }

    private void bindAction() {
        tv_classify.setOnClickListener(this);
        ll_msg.setOnClickListener(this);
        rl_search.setOnClickListener(this);
    }

    public void refreshMsgCount(int count) {
        if (count > 0) {
            if (null == bv_msg_count) {
                bv_msg_count = new BadgeView(getContext(), iv_msg_flag);
                bv_msg_count.setBadgeMargin(0, 0);
                bv_msg_count.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
                bv_msg_count.setTextSize(10);
            }

            bv_msg_count.setText(TipCountUtil.cover(count, TipCountUtil.SMALL_FLAG));
            bv_msg_count.show();
        } else {
            if (null != bv_msg_count) {
                bv_msg_count.hide();
            }
        }
    }

    private Context getContext() {
        return iv_msg_flag.getContext();
    }

    @Override
    public void onClick(View v) {
        onOptionClickListener.clickOption(v, v.getId());
    }

    public void changeHeaderbg(int x, int y, int oldx, int oldy) {
        // 渐变效果实现 向上移动的距离超过200dp -> 颜色变成透明色
        int MAXHEIGHT = DensityUtils.dip2px(getContext(), 200);
        if (y <= 0) {   //设置标题的背景颜色
            mHomeTopView.setBackgroundColor(getContext().getResources().getColor(R.color.transparent));
        } else if (y > 0 && y <= MAXHEIGHT) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
            float scale = (float) y / MAXHEIGHT;
            float alpha = (255 * scale);
            mHomeTopView.setBackgroundColor(Color.argb((int) alpha, 177, 37, 36));
        } else {    //滑动到banner下面设置普通颜色
            mHomeTopView.setBackgroundColor(getContext().getResources().getColor(R.color.title_bg));
        }
    }

    public interface OnOptionClickListener {
        void clickOption(View v, int optionId);
    }
}